package cn.edu.stu.syllabus.utils;

import cn.edu.stu.syllabus.application.SyllabusApplication;
import cn.edu.stu.syllabus.dao.DaoMaster;
import cn.edu.stu.syllabus.dao.DaoSession;

/**
 * Created by daidaijie on 2017/3/19.
 * GreenDao操作基类
 */

public class GreenDaoUtils {

    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;
    private DaoMaster.DevOpenHelper mDevOpenHelper;


    private GreenDaoUtils() {
        mDevOpenHelper = new DaoMaster.DevOpenHelper(SyllabusApplication.getContext(), "syllabus-db");
        mDaoMaster = new DaoMaster(mDevOpenHelper.getWritableDatabase());
        mDaoSession = mDaoMaster.newSession();
    }

    private static class GreenDaoUtilsHolder {
        private static GreenDaoUtils sGreenDaoUtils = new GreenDaoUtils();
    }

    public static GreenDaoUtils getInstance() {
        return GreenDaoUtilsHolder.sGreenDaoUtils;
    }

    public DaoMaster getMaster() {
        return mDaoMaster;
    }

    public DaoSession getSession() {
        return mDaoSession;
    }

    /**
     * 关闭数据连接
     */
    public void close() {
        if (mDevOpenHelper != null) {
            mDevOpenHelper.close();
        }
    }
}
